package com.shop.cereshop.app.controller.feedback;

import com.shop.cereshop.app.annotation.NoRepeatSubmit;
import com.shop.cereshop.app.annotation.NoRepeatWebLog;
import com.shop.cereshop.app.service.feedback.FeedbackService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.constant.FeedbackConstant;
import com.shop.cereshop.commons.constant.FeedbackConstant.UserType;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.param.feedback.SubmitFeedbackDTO;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @description: TODO
 * @author lx
 * @date 2025/11/19 09:52
 * @version 1.0
 */
@RestController
@RequestMapping("/feedback")
@Api(tags = "用户反馈")
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    @PostMapping("/submit")
    @ApiOperation(value = "用户反馈")
    @NoRepeatSubmit
    @NoRepeatWebLog
    public Result<Object> submit(@RequestBody @Valid SubmitFeedbackDTO param, HttpServletRequest request) throws CoBusinessException {
        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (user == null) {
            return Result.unauthorized();
        }
        // 使用统一的用户类型定义
        param.setUserType(UserType.USER);
        param.setUserId(user.getBuyerUserId());
        int row = feedbackService.submitFeedback(param);
        return row > 0 ? Result.success() : Result.failed(CoReturnFormat.FAIL);
    }
}
